package com.ninong.ker.admin.rest;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ninong.ker.admin.service.LoginService;
import com.ninong.ker.admin.service.dto.UserDTO;
import com.ninong.ker.common.annotation.log.Log;
import com.ninong.ker.common.auth.JwtProperties;
import com.ninong.ker.common.response.CmdResponse;
import com.ninong.ker.common.tools.Constants;
import com.ninong.ker.common.tools.RedisUtils;
import com.ninong.ker.common.tools.enums.BusinessEnum;
import com.wf.captcha.ArithmeticCaptcha; 

/**
 * 登录
 * 
 * @author cmd
 *
 */
@RestController
@RequestMapping("/cmd/v1/auth")
public class LoginResouces {

	@Autowired
	private LoginService loginService;
	
	@Autowired
	private JwtProperties properties;
	
	@Autowired
	private RedisUtils redisUtils;

	@Value("${login.login-code.expiration}")
	private long expiration;
	
	/**
	 * 登录
	 * @param request
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@Log("登录")
	@PostMapping(value = "/login")
	public ResponseEntity login(HttpServletRequest request, @RequestBody UserDTO dto) throws Exception {
		return ResponseEntity.ok(loginService.login(dto));
	}
	
	
	/**
	 * 合法菜单
	 * @param request
	 * @param token
	 * @return
	 */
	@GetMapping(value = "/menus")
	public ResponseEntity menus(HttpServletRequest request, @RequestHeader(value = Constants.TOKEN) String token)  {
		CmdResponse response = new CmdResponse();
		response.setData(loginService.listMenus(token));
		return ResponseEntity.ok(response);
	}
	
	/**
	 * 用户信息
	 * @param token
	 * @return
	 */
	@GetMapping(value = "/info")
	public ResponseEntity info(@RequestHeader(value = Constants.TOKEN) String token)  {
		CmdResponse response = new CmdResponse();
		response.setData(loginService.listInfo(token));
		return ResponseEntity.ok(response);
	}
	
	/**
	 * 在线用户
	 * @return
	 */
	@PostMapping(value = "/online")
	public ResponseEntity online(@RequestBody UserDTO dto)  {
		CmdResponse response = new CmdResponse();
		response.setData(loginService.listOnline(dto));
		return ResponseEntity.ok(response);
	}
	
	/**
	 * 驱逐某个在线用户
	 * @param ids
	 * @return
	 */
	@PostMapping(value = "/expel")
	public ResponseEntity expel(@RequestBody String[] ids)  {
		CmdResponse response = new CmdResponse();
		loginService.removeOnline(ids);
		response.setMessage(BusinessEnum.DELETE_SUCCESS.getMsg());
		return ResponseEntity.ok(response);
	}
	
	@GetMapping(value = "/code")
	public ResponseEntity<Object> getCode() {
		ArithmeticCaptcha captcha = new ArithmeticCaptcha(111, 36);
		// 几位数运算，默认是两位
		captcha.setLen(2);
		// 获取运算的结果
		String result = captcha.text();
		String uuid = properties.getCode() + UUID.randomUUID().toString();
		// 保存
		redisUtils.set(uuid, result, expiration, TimeUnit.MINUTES);
		// 验证码信息
		@SuppressWarnings("serial")
		Map<String, Object> imgResult = new HashMap<String, Object>(2) {
			{
				put("img", captcha.toBase64());
				put("uuid", uuid);
			}
		};
		CmdResponse response = new CmdResponse();
		response.setData(imgResult);
		return ResponseEntity.ok(response);
	}

}
